//
//  LYGeneralSetViewController.m
//  LY
//
//  Created by lanou3g on 15/12/15.
//  Copyright © 2015年 梁媛. All rights reserved.
//

#import "LYGeneralSetViewController.h"
#import "UIImageView+WebCache.h"
static BOOL flag = NO;
@interface LYGeneralSetViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSArray *array;

@end

@implementation LYGeneralSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.generalSet.delegate = self;
    self.generalSet.dataSource = self;
    if (flag) {
        self.array = @[@"清除缓存",@"白天模式"];
    }else{
        self.array = @[@"清除缓存",@"夜间模式"];
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftAction)];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)leftAction{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == 1) {
        flag = !flag;
        if (flag) {
            [[UIApplication sharedApplication].delegate window].alpha = 0.6;
            cell.textLabel.text = @"白天模式";
        }else{
            [[UIApplication sharedApplication].delegate window].alpha = 1;
            cell.textLabel.text = @"夜间模式";
        }
    }else if (indexPath.row == 0){
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *str = [self fileSizeAtPath:path];
        if (![str isEqualToString:@"0B"]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"缓存大小为%@",str] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self clearCache:str];
                cell.detailTextLabel.text = nil;
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有缓存" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            [controller addAction:action];
            [self presentViewController:controller animated:YES completion:nil];
        }
    }
}

-(NSString*)fileSizeAtPath:(NSString*)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    CGFloat size = 0;
    NSString *sizeStr = nil;
    if([fileManager fileExistsAtPath:path]){
        size = [SDImageCache sharedImageCache].getSize;
        if (size < 1024) {
            sizeStr = [NSString stringWithFormat:@"%.fB",size];
        }else if ((size >= 1024) && (size < 1024 * 1024)){
            sizeStr = [NSString stringWithFormat:@"%.fKB",size / 1024];
        }else {
            sizeStr = [NSString stringWithFormat:@"%.2fM",size / 1024 / 1024];
        }
    }
    return sizeStr;
}

-(void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] clearDisk];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reused = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reused];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reused];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        cell.detailTextLabel.text = [self fileSizeAtPath:path];
    }
    cell.textLabel.text = self.array[indexPath.row];
    return cell;
}

- (UITableView *)generalSet{
    if (!_generalSet) {
        _generalSet = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 140) style:(UITableViewStylePlain)];
        [self.view addSubview:_generalSet];
    }
    return _generalSet;
}

@end
